package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    @Transactional
    /*新增套餐和对应的菜品*/
    public void savewithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);
      //向套餐包含的菜品表中插入n条数据
        Long setmealId=setmeal.getId();
        List<SetmealDish> setmealDishes=setmealDTO.getSetmealDishes();
            setmealDishes.forEach( setmealDish-> {setmealDish.setSetmealId(setmealId);});
        //保存套餐和菜品的关联关系
            setmealDishMapper.insertBatch(setmealDishes);
    }
    /*套餐分页查询*/
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO>page=setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }
   /*套餐批量删除*/
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //判断套餐是否在起售中 只要有一个在就不能删除
        for(Long id:ids){
          Setmeal setmeal=setmealMapper.getById(id);
          if(setmeal.getStatus()== StatusConstant.ENABLE)
          {
              throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
          }
        }
         //删除套餐
        setmealMapper.deleteByIds(ids);
        //删除套餐菜品关系表下的数据
        setmealDishMapper.deleteByIds(ids);
    }
   /*修改套餐第一步 --根据id查找套餐及对应菜品(回显)*/
    @Override
    public SetmealVO getByIdWithDish(Long id) {
        Setmeal setmeal=setmealMapper.getById(id);
        //根据套餐id查询套餐对应的菜品
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        //封装
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }
    /*修改套餐*/
    @Override
    @Transactional
    public void updateWithDish(SetmealDTO setmealDTO) {
        //修改套餐的基本信息
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //修改套餐下的菜品
        //先删除原有菜品，再插入新菜品
        setmealDishMapper.deleteById(setmeal.getId());
        List<SetmealDish>setmealdishes=setmealDTO.getSetmealDishes();
        if(setmealdishes.size()>0)
        {
            setmealdishes.forEach(setmealDish -> {setmealDish.setSetmealId(setmeal.getId());});
            setmealDishMapper.insertBatch(setmealdishes);
        }
    }
     /*修改套餐状态 */
    @Override
    @Transactional
    public void updateStatus(Integer status, Long id) {
        //起售套餐时，判断套餐内是否有停售菜品，有停售菜品提示"套餐内包含未启售菜品，无法启售"
        if(status==StatusConstant.ENABLE)
        {
            List<Dish>dishList=dishMapper.getBysetmealId(id);
            if(dishList != null && dishList.size() > 0){
            dishList.forEach(dish -> {
                if(StatusConstant.DISABLE == dish.getStatus()){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }
        }

        Setmeal setmeal=Setmeal.builder().
                        id(id).
                        status(status).
                        build();
        setmealMapper.update(setmeal);
    }
       /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }
     /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }



}
